#include <map>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
};

class Solution
{
public:
    ListNode *removeZeroSumSublists(ListNode *head)
    {
        map<int, ListNode *> m;
        ListNode *dummyHead = new ListNode;
        dummyHead->next = head;
        m.insert({0, dummyHead});

        ListNode *cur = head;
        int sum = 0;
        while (cur)
        {
            sum += cur->val;
            if (m.find(sum) != m.end())
            {
                auto tmp = m.find(sum);
                tmp->second->next = cur->next;
            }
            else
            {
                m.insert({sum, cur});
            }
            cur = cur->next;
        }
        return dummyHead->next;
    }
};